// pages/today/index.js
const util = require('../../utils/util.js')

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    profit: "0",
    date: "",
    amount: "0",
    balance: "0",
    list: {},
    openid: ""
  },

  getname: function() {
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        wx.request({
          url: 'http://192.168.3.2/cloudwarehouse/today/getname.php',
          data: {
            openid: res.data,
            today: util.getNowFormatDate()
          },
          success: function(res) {
            console.log(res)

            that.setData({
              list: res.data
            })
          }
        })
      },
    })



  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      date: util.getNowFormatDate(),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    wx.getStorage({                                 //七天统计数据
      key: 'openid',
      success: function(res) {
        wx.request({
          url: 'http://192.168.3.2/cloudwarehouse/analysis/day.php',
          data: {
            today: util.getNowFormatDate(),
            openid: res.data
          },
          success: function(res) {
            app.globalData.today1 = res.data

          }
        })
      }
    })

    wx.getStorage({       
      key: 'openid',                           //五周统计数据
      success: function (res) {
        wx.request({
          url: 'http://192.168.3.2/cloudwarehouse/analysis/week.php',
          data: {
            today: util.getNowFormatDate(),
            openid: res.data
          },
          success: function (res) {
              console.log(res.data)
            app.globalData.today2 = res.data
          }
        })
      }
    })

    wx.getStorage({
      key: 'openid',                           //五周统计数据
      success: function (res) {
        wx.request({
          url: 'http://192.168.3.2/cloudwarehouse/analysis/month.php',
          data: {
            today: util.getNowFormatDate(),
            openid: res.data
          },
          success: function (res) {
            console.log(res.data)
            app.globalData.today3 = res.data
          }
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var openid;
    this.getname();
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        wx.request({
          url: 'http://192.168.3.2/cloudwarehouse/today/getstatus.php',
          data: {
            date: that.data.date,
            yesterday: that.yesterday(),
            openid: res.data
          },
          success(ress) {
            that.setData({
              profit: ress.data[0],
              balance: ress.data[1],
              amount: ress.data[2]
            })
          }
        })

      },
    })


    wx.hideLoading()


  },


  yesterday: function() {
    var date1 = new Date();
    var date7 = new Date(date1.getTime() - 24 * 60 * 60 * 1000);

    var seperator1 = "-";
    var year = date7.getFullYear();
    var month = date7.getMonth() + 1;
    var strDate = date7.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})